package com.hc.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.hc.bean.BSizeBean;
import com.hc.comm.CommData;

public class BSizeConfigUtil {

	
	public static final String FILEPATH=CommData.systemPath+"config\\bSizeGradeConfig.properties";
	
	public static List<BSizeBean> readConfig(){
		
		List<BSizeBean>  list = new ArrayList<BSizeBean>();
	File file = new File(FILEPATH);
		
		FileReader fileReader = null;

			try {
				fileReader = new  FileReader(file);
			} catch (FileNotFoundException e1) {
					e1.printStackTrace();
			}
			BufferedReader reader = new BufferedReader(fileReader);
			String linStr = "";
			
			BSizeBean bean = null;
			
			try {
				
				int i =0;
				
				while((linStr=reader.readLine())!=null){
				
					i++;
					
				String[] results= 	linStr.split("=");
					
		
				
					if("name".equals(results[0])){
						
						bean = new BSizeBean();
						
						
						bean.setId(System.currentTimeMillis()+i);
						bean.setName(results[1]);
						
//						System.out.println(bean.getName()+"___________"+bean.getId());
						
					}else if("maxNumber".equals(results[0])){
						
						bean.setMaxSpeed(Double.parseDouble(results[1]));
						
					}else if("minNumber".equals(results[0])){
						
						bean.setMinSpeed(Double.parseDouble(results[1]));
						list.add(bean);
					}
				}
				
				reader.close();
				fileReader.close();
			} catch (IOException e) {
					e.printStackTrace();
			}
		
		return list;

	}
	
	public static boolean writeConfig(List<BSizeBean> list){
		StringBuilder sb = new StringBuilder();
		for(BSizeBean bean:list){
			
			
			sb.append("name=").append(bean.getName()).append("\r\n");
			sb.append("maxNumber=").append(bean.getMaxSpeed()).append("\r\n");
			sb.append("minNumber=").append(bean.getMinSpeed()).append("\r\n");
		}
		
		File file = new File(FILEPATH);
		FileWriter w;
		try {
			w = new FileWriter(file);
			w.write(sb.toString());
			w.flush();
			w.close();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	
		return true;

	}
	public static void main(String[] args) {
		List<BSizeBean> beans = BSizeConfigUtil.readConfig();
		for(BSizeBean bean:beans){
			
			
			System.out.println(bean);
			
		}
	
	}
}
